<?php
/**
 * @version      BOSSCMS-Free
 * @trademark    BOSSCMS
 * @copyright    Copyright © by BOSSCMS-Free (https://www.bosscms.net/) 保留所有权利
 * @license      https://gitee.com/Greenpeas/BOSSCMS/license/ 和 https://www.bosscms.net/freelicense/
 * @link         https://www.bosscms.net/
 * @author       温州软界网络技术有限公司
**/
defined('IS_OK') or exit('Access Forbidden');

into::basic_class('web');

class feedback extends web
{
	public function form()
	{
		global $G;
		if(isset($G['post'])){
			$items = arrExist($G['post'],'items');
			if(!is_numeric($items) && $items){
				die();
			}
			$data = page::config_option($items);
			$content = '';
			if($data){
				$G['config'] = array_merge($data, $G['config']);
			}
			$form = page::form($items);
			if($G['config']['feedback_open']){
				if($G['config']['feedback_captcha'] && (!session::get('captcha') || arrExist($G['post'],'captcha')!=session::get('captcha'))){
					alert($G['config']['feedback_captcha_error']);
				}
				$uping = 0;
				foreach($form as $v){
					$value = $G['post']['params'.$v['id']];
					if(is_array($value) && !preg_match('/^[67]$/',$v['style'])){
						alert($v['title'].'内容错误');
					}
					if($v['must']){
						if(($v['style']==8 && $_FILES['params'.$v['id']]['error']!=0) || ($v['style']!=8 && !$value)){
							alert($v['title'].'不能为空');
						}
						if(preg_match('/^[35]$/',$v['style'])){
							if(!in_array($value,json::decode($v['param']))){
								alert($v['title'].'内容错误');
							}
						}
						if($v['style']==6){
							$p = json::decode($v['param']);
							foreach($value as $v1){
								if(!in_array($v1,$p)){
									alert($v['title'].'内容错误');
								}
							}
						}
					}
					if($v['rule'] && preg_match('/^[0124]$/',$v['style'])){
						if(!preg_match($v['rule'],$value)){
							alert($v['title'].'验证失败');
						}
					}
					if($v['style']==8){
						$uping++;
					}
				}
				$post = array();
				foreach($form as $v){
					$post['params'.$v['id']] = $str = $G['post']['params'.$v['id']];
					$content .= "<tr>
					  <td><b>{$v['title']}</b>&nbsp;</td>
					  <td><p>".(is_array($str)?implode('&emsp;',$str):$str)."</p></td>
					</tr>";
				}
				if($content){
					$content = '<table>'.$content.'</table>';
				}
				$data = array(
					'param' => json::enfilter($post),
					'parent' => $items,
					'reply' => '',
					'manager' => '0',
					'ctime' => TIME,
					'mtime' => 0,
					'ip' => getIP(),
					'display' => 0
				);
				$res = mysql::select_one('ctime','feedback',"ip='{$data['ip']}'",'ctime DESC,id DESC');
				if($data['ctime'] - $res['ctime'] > 60){
					if(mysql::insert($data, 'feedback')){
						alert($G['config']['feedback_success'], $_SERVER['HTTP_REFERER']);
					}else{
						alert('提交失败！');
					}
				}else{
					alert($G['config']['feedback_quick']);
				}
			}else{
				alert('反馈表单处于关闭状态，请启用！');
			}
		}
	}
}
?>